package com.sptpc.demo.controller;

import com.sptpc.demo.controller.requests.ModifyStudentRequest;
import com.sptpc.demo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
    @GetMapping("/")
    public List<Student> getAll(){
        List<Student> results = new ArrayList<>();
        results.add(Student.builder()
                .id(1L).name("小明")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("软件技术").build());
        results.add(Student.builder().id(2L).name("小兰")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("信安").build());
        results.add(Student.builder().id(3L).name("小杨")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("云计算").build());
        results.add(Student.builder().id(4L).name("小花")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("大数据").build());
        return results;
    }
    @PostMapping("/")
    public Student addNewStuend(@RequestBody Student stuend) {
        log.info("新增学生信息：{}",stuend);
        Student addStuend = Student.builder().name("周晓梅").id(9L)
                .adminssionDate(LocalDate.parse("2020-10-15"))
                .major("移动应用开发").build();
        return addStuend;
    }
    @DeleteMapping("/{id}")
    public String deleteStuend(@PathVariable Integer id){
        log.info("要删除的学生信息id:{}",id);
        return "删除成功";
    }
    @GetMapping("/{id}")
    public Student getStuend(@PathVariable Integer id){
        log.info("查询学生的id:{},{}",id);
        Student stuend = Student.builder().id(1L).name("小明")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("软件技术").build();
        return stuend;
    }
    @PutMapping("/")
    public Student modify(@RequestBody ModifyStudentRequest request){
        log.info("要修改的学生信息:{}",request);
        Student addStuend = Student.builder().id(1L).name("小明")
                .adminssionDate(LocalDate.parse("2019-09-01"))
                .major("软件技术").build();
        return addStuend;
    }
    @PostMapping(value = "/batch",
            consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> batchAddNewStudent(MultipartFile file){
        List<Student> results=new ArrayList<>();
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(
                    file.getInputStream()));
            String line="";
            while ((line=reader.readLine())!=null) {
                //解析一行
                String[] args=line.split(",");
                Student student=Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .adminssionDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build();
                results.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }



}
